package leetcode.code0811;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public List<String> subdomainVisits(String[] cpdomains) {
		Map<String, Integer> map = new HashMap<>();
		for (int i = 0; i < cpdomains.length; i++) {
			String domains = cpdomains[i];
			String[] infos = domains.split(" ");
			int times = Integer.valueOf(infos[0]);
			String doms = infos[1];
			int index = -1;
			do {
				map.put(doms, map.getOrDefault(doms, 0) + times);
				index = -1;
				if ((index = doms.indexOf(".")) > 0) {
					doms = doms.substring(index + 1);
				}
			} while (doms.length() > 0 && index != -1);
		}
		List<String> ans = new ArrayList<>();
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			ans.add(entry.getValue() + " " + entry.getKey());
		}
		return ans;
	}

	@Override
	public void debug4() {
		this.subdomainVisits(H.str2array2("[\"9001 discuss.leetcode.com\"]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
